//
//  NetworkManager.m
//  VTravel
//
//  Created by huangzhen on 16/6/1.
//  Copyright © 2016年 huangzhen. All rights reserved.
//

#import "NetworkManager.h"
#import <AFNetworking.h>
#import <CommonCrypto/CommonHMAC.h>
#import<CommonCrypto/CommonCryptor.h>

@implementation NetworkManager


// 参数urlStr表示网络请求url, paramdic 表示请求参数, finish回调指网络请求成功回调, enError表示失败回调
// GET请求
+ (void)requestGETWithURLStr:(NSString *)urlStr paramDic:(NSDictionary *)paramDic finish:(void(^)(id responseObject))finish enError:(void(^)(NSError *error))enError {
    
    // 创建一个sessionManager对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 指定我们我们能够解析的数据类型包含text/html
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", nil];
    
    // AFNetworking请求结果回调是, failure方法会在两种情况下回调: 1.请求服务器失败, 服务器返回失败信息; 2.服务器返回时间成功, AFN解析返回数据失败
    [manager GET:urlStr parameters:paramDic success:^(NSURLSessionDataTask *task, id responseObject) {
        finish(responseObject);

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        enError(error);

    }];
//    [manager GET:urlStr parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        finish(responseObject);
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {// 触发: 数据请求失败 或者 数据请求成功,但是解析失败(只解析JSON数据)
//        
//        enError(error);
//        
//    }];
//}
}
// POST请求
+ (void)requestPOSTWithURLStr:(NSString *)urlStr paramDic:(NSDictionary *)paramDic finish:(void(^)(id responseObject))finish enError:(void(^)(NSError *error))enError {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 指定我们我们能够解析的数据类型包含text/html
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", nil];
    [manager POST:urlStr parameters:paramDic  success:^(NSURLSessionDataTask *task, id responseObject) {
        finish(responseObject);

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        enError(error);
    }];
//    
//    [manager POST:urlStr parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        finish(responseObject);
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//        enError(error);
//    }];
    
}

+ (void)cancel {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.operationQueue cancelAllOperations];
}
+ (NSString *) md5:(NSString *)str {
    if (str == nil) {
        return nil;
    }
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result);
    return [NSString stringWithFormat:
            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
//    NSLog(@"++%@",[NSString stringWithFormat:
//                   @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
//                   result[0], result[1], result[2], result[3],
//                   result[4], result[5], result[6], result[7],
//                   result[8], result[9], result[10], result[11],
//                   result[12], result[13], result[14], result[15]
//                   ]);
}

@end
